<?php
//multiple crops, one or mult attributes, weekly subview
?>

<thead>

<?php
include '../../functions.php';
include '../../additionalFunctions.php';

$crops = explode(";",$_GET['crops']);

$cropsFormatted = array();
$units = array();
foreach ($crops as $c) {
	$cropsFormatted[] = substr($c, 0, strripos($c, " - ")); // finds the last position of occurence of " - " ==> takes only the crop name, not the units
	$units[] = substr($c, strripos($c, " - ") + 3); // gets just the name of the unit
}
$crops = $cropsFormatted;

$month = $_GET['month'];
$year = $_GET['year'];
$attributes = $_GET['attributes'];
$attributesArray = explode(';',$attributes);
$selectedA = $_GET['selectedA'];

print "<tr><th></th><th>Week</th>";

foreach ($crops as $c) {
	print "<th>$c</th>";
}

$result = array();
for ($i = 0; $i < count($crops); $i++) {
	$result[$i] = getRangeWeeklyCropData($year."-".getMonthNumber($month)."-01",$year."-".getMonthNumber($month)."-".lastDayOfMonth(getMonthNumber($month)),getCropId($crops[$i]),$selectedA, getUnitId($units[$i]), $selectedA == 'sales' ? getCustomerId($_GET['customers']) : ($selectedA == 'amount_donated' ? getCustomerId($_GET['donationCustomers']) : NULL));
}

$weeksArray = array();
foreach ($result as $indexOfCrop => $resultForCrop) {
	foreach ($resultForCrop as $week => $value) {
		$weeksArray[] = $week;
	}
}
$weeksUnique = array_unique($weeksArray);
foreach ($weeksUnique as $w) {
	print "<tr><td id=\"td1\"></td><td id=\"td2\">";
	print $w;
	print "</td>";
	for ($j = 0; $j < count($crops); $j++) {
		$l = $j + 3;
		$dayStart = substr($w, 3 , 2);
		$dayEnd = substr($w, 11 , 2);
		$monthStart = substr($w, 0 , 2);
		$monthEnd = substr($w, 8 , 2);
		$result = getRangeData($year."-".$monthStart."-".$dayStart,$year."-".$monthEnd."-".$dayEnd,getCropId($crops[$j]),$selectedA, getUnitId($units[$j]), $selectedA == 'sales' ? getCustomerId($_GET['customers']) : ($selectedA == 'amount_donated' ? getCustomerId($_GET['donationCustomers']) : NULL));
		print "<td id=\"td".$l."\">".$result."</td>";
	}
	print "</tr>";
}
	
?>
</tbody>